package com.app.utils;


import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.app.model.WechatMessageTemplate;
import com.app.service.impl.WechatMessageTemplateServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.aspectj.lang.annotation.After;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WechatSubscriptionMessage {


    @Autowired
    public WechatMessageTemplateServiceImpl wechatMessageTemplateService;



    public JSONObject sendWeChatMessageSubscriptionNotifications(String openId, String template_type, List<String> paramsData) throws Exception {
        JSONObject result = new JSONObject();
        QueryWrapper<WechatMessageTemplate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("template_type", template_type);
        WechatMessageTemplate wechatMessageTemplate = wechatMessageTemplateService.getOne(queryWrapper);
        if (wechatMessageTemplate == null) {
            result.put("code", 1);
            result.put("msg", "消息模板不存在");
            return result;
        }
        String message_key = wechatMessageTemplate.getMessageKey();
        List<String> messageKeys = Arrays.asList(message_key.split(","));
        if (messageKeys.size() != paramsData.size()){
            result.put("code", 1);
            result.put("msg", "参数数量与模板key值不对应");
            return result;
        }

        String accessToken = WechatUtil.redisToken();
        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken;
        JSONObject data = new JSONObject();
        data.put("touser", openId);
        data.put("template_id", wechatMessageTemplate.getTemplateId());
        data.put("page", "index");

        JSONObject params = new JSONObject();
        for (int i = 0; i < messageKeys.size(); i++){
            JSONObject thing = new JSONObject();
            thing.put("value", paramsData.get(i));
            params.put(messageKeys.get(i), thing);
        }
        data.put("data", params);
        String resultStr = HttpUtil.post(url, data.toJSONString());
        result = JSONObject.parseObject(resultStr);
        return result;
    }




    private static final String TEMPLATE_ID = "m1Dj9Z5-AYSFKZ3Lm7yu5VFYdNAeVcnIK7L5dyJzcHE";

    public static String sendTemplateMessage(String openId)throws Exception {
        String accessToken = WechatUtil.redisToken();
//        String accessToken = "83_KucFXmiPwquNNaZIySdWWD0-E2nc53I95JBC6Nl9IgTZC7KoTq0PiiNvc330IAMHJTMLjdCgivd2ksCJHAXzruFig8RU1TuHtEpUu-CW1PLFQ4lRDV0iYmR7JjUOHMdAIAATY";
        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken;
        JSONObject data = new JSONObject();
        data.put("touser", openId);
        data.put("template_id", TEMPLATE_ID);
        data.put("page", "index");


        // 设置消息内容
        JSONObject thing1 = new JSONObject();
        thing1.put("value", "请假人");
        JSONObject thing2 = new JSONObject();
        thing2.put("value", "请假类型");

        JSONObject time3 = new JSONObject();
        time3.put("value", "2019年10月1日 15:01");
        JSONObject time4 = new JSONObject();
        time4.put("value", "2019年10月1日 15:01");
        JSONObject object= new JSONObject();
        object.put("thing1", thing1);
        object.put("thing2", thing2);
        object.put("time3", time3);
        object.put("time4", time4);
        data.put("data", object);
        String result = HttpUtil.post(url, data.toJSONString());
        System.out.println("发送订阅消息结果：" + result);
        return result;
    }

}
